package org.example.seckilljava.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.seckilljava.common.Result;
import org.example.seckilljava.dto.SecKillDTO;
import org.example.seckilljava.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/api/seckill")
@Slf4j
public class SecKillController {

    @Autowired
    private SecKillService secKillService;

    private static final AtomicLong requestCount = new AtomicLong();


    @PostMapping("/dov1")
    public Result<String> doSecKill_v1(@RequestBody SecKillDTO secKillDTO) {
        String orderNum = secKillService.doSecKill_V1(secKillDTO);
        if (orderNum != null) {
            return Result.success(orderNum);
        } else {
            return Result.error("秒杀失败");
        }
    }

    @PostMapping("/dov2")
    public Result<String> doSecKill_v2(@RequestBody SecKillDTO secKillDTO) {
        String orderNum = secKillService.doSecKill_V2(secKillDTO);
        requestCount.incrementAndGet();
        if (orderNum != null) {
            return Result.success(orderNum);
        } else {
            return Result.error("秒杀失败");
        }
    }

    @PostMapping("/dotest")
    public Result<String> doTest(@RequestBody SecKillDTO secKillDTO) {
        System.out.println(requestCount.get());
        return Result.success("测试api的极限rps");
    }
} 